import {createRouter, createWebHashHistory, RouterOptions} from 'vue-router'

//导入组件
import UserList from '@/components/users/UserList.vue'
import UserDetails from '@/components/users/UserDetails.vue'
import ToDoView from '@/components/todos/ToDoView.vue'
import ProductView from '@/components/products/ProductView.vue'
import Login from '@/components/Login.vue'
import Home from '@/components/Home.vue'
import Layout from '@/components/Layout.vue'
import BaseHome from '@/components/BaseHome.vue'

export const routes = [
    { path: "/", redirect: "/login" },
    { path: "/login", name: "登录", component: Login },
    { path: "/home", name: "首页", icon: "HomeFilled", component: Home,
    children: [
            { path: "/user", name: "信息管理", icon: "User", component: Layout, redirect: "/user/manage-user",
                children: [
                    { path: "userDetails", name: "用户详情", icon: "HomeFilled", component: UserDetails, hidden: true },
                    { path: "manage-user", name: "用户管理", icon: "UserFilled", component: UserList },
                    { path: "manage-article", name: "ToDos列表", icon: "Document", component: ToDoView },
                    { path: "manage-comment", name: "商品列表", icon: "Goods", component: ProductView },
                    { path: "manage-image", name: "图片管理", icon: "Picture", component: BaseHome }
                ]
            }
        ]
    },
]

const router = createRouter(<RouterOptions>{
    history: createWebHashHistory(),
    routes
})

// 配置路由导航守卫
router.beforeEach((to, from, next) => {
    if (to.path === '/login') return next()
    if (to.path === '/home' || to.path.toString().includes("user")) {
        const token = localStorage.getItem('token')
        if (!token) return next('/login')
    }
    next()
})

export default router

